package com.contory.ssn_lgnd.app;

import android.opengl.GLES20;

import com.contory.ssn_lgnd.framework.gl.Camera2D;
import com.contory.ssn_lgnd.framework.gl.ESShader;
import com.contory.ssn_lgnd.framework.gl.FrameBufferObject;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.Texture;
import com.contory.ssn_lgnd.framework.gl.TexturePtr;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;

public abstract class EffectHelper {
	final FrameBufferObject fb;
	final ESShader shader;
	final Camera2D cam;
	final SpriteBatcher batcher;
	
	public EffectHelper(FrameBufferObject fb, ESShader shader, Camera2D cam, SpriteBatcher batcher){
		this.fb = fb; this.shader = shader; this.cam = cam; this.batcher = batcher;
	}
	
	public void bind(){
		fb.bind();
		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
		cam.setViewPort(fb);
		shader.bind();
		cam.setMatrices(shader, ESShaderHandleNames.Uni_MvpMatrix);
		bindUniforms();
	}
	
	protected abstract void bindUniforms();
	
	public void unBind(){
		fb.unBind();
	}
	
	public void beginBatch(TexturePtr dst){
		batcher.beginBatch(shader, dst, ESShaderHandleNames.Uni_Sample0, 0);
	}
	
	public void beginBatch(TexturePtr dst, TexturePtr src){
		batcher.beginBatch(shader, dst, ESShaderHandleNames.Uni_Sample0, 0, 
				src, ESShaderHandleNames.Uni_Sample1, 1);
	}
	
	public void endBatch(){
		batcher.endBatch(shader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
	}
	
	public TexturePtr getTexture(){
		return fb.getTexture();
	}
	
	public TextureRegion getTextureRegion(){
		return fb.getTextureRegion();
	}
}
